﻿

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;


using Gift.Model;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;

namespace WebApp.admin.basic
{
    public partial class freightWayList : BaseAdmin
    {
     
        protected string kw = "";
        protected string order = "id ";
        protected int ot = 0;
        protected int pageCount = 0;
        protected int pageIndex = 1;
        protected int pageSize = 20;
        protected PageList pl = new PageList();

        tbFreightWay oFreightWay;

        protected int t = 0;
        protected int total = 0;

        bool _isEdit = false;
        bool _isDeleted = false;

        protected string tid
        {
            get
            {
                return Request.QueryString["tid"];
            }
        }
        protected string gid
        {
            get
            {
                return Request.QueryString["gid"];
            }
        }


        // Methods
        private void FreightTemplateBind()
        {
            try
            {
                if (base.Request.QueryString["p"] != null)
                {
                    this.pageIndex = int.Parse(base.Request.QueryString["p"]);
                }
                if (base.Request.QueryString["ps"] != null)
                {
                    this.pageSize = Convert.ToInt32(base.Request.QueryString["ps"]);
                }
                if (base.Request.QueryString["or"] != null)
                {
                    this.order = base.Request.QueryString["or"];
                }
                if (base.Request.QueryString["ot"] != null)
                {
                    this.ot = Convert.ToInt32(base.Request.QueryString["ot"]);
                }
                this.pl.TblName = "tbFreightWay";
                this.pl.StrGetFields = " id,(select freightTemplateName from tbFreightTemplate where id= freightTemplate) as freightTemplate,"
                                    + " (select deliveryWayName from tbDeliveryWay where id = deliveryWay) as deliveryWay, "
                                    + " regionValues =  stuff((select ',' +  regionName from tbRegion  where charindex(',' + convert(varchar(10),id ) + ',',',' + tbFreightWay.region + ',') >0  for xml path('')) , 1 , 1 , '')"
                                    + " , defaultFreight, increasedFreight, companyId,convert(nvarchar(10),createTime,121) as createTime";
                this.pl.FldName = this.order;
                this.pl.PageSize = this.pageSize;
                this.pl.PageIndex = this.pageIndex;
                this.pl.DoCount = 1;
                this.pl.OrderType = this.ot;
                if ((base.Request.QueryString["t"] != null) && (base.Request.QueryString["kw"] != null))
                {
                    this.kw = base.Request.QueryString["kw"];
                    this.pl.StrWhere = base.Request.QueryString["t"] + " like '%" + this.kw + "%' and companyId=" + base.Administrator.CompanyId + "";
                }
                else
                {
                    this.pl.StrWhere = "companyId=" + base.Administrator.CompanyId + "";
                }

                if (base.Request.QueryString["tid"] != null)
                {
                    this.pl.StrWhere = this.pl.StrWhere + " and freightTemplate = " + Request.QueryString["tid"];
                }
                this.total = Convert.ToInt32(BaseFactory.thePageList.GetPageList(this.pl).Rows[0]["Total"]);
                this.pl.DoCount = 0;
                DataTable pageList = BaseFactory.thePageList.GetPageList(this.pl);
                new PageListNo().DelPageSize(pageList, this.total);
                if (pageList.Rows.Count == 0)
                {
                    this.btnDelete.Visible = false;
                }
                this.rptFreightWay.DataSource = pageList.DefaultView;
                this.rptFreightWay.DataBind();
            }
            catch
            {
            }
        }

        void ValidateLoginUser()
        {
            _isEdit = true;
            _isDeleted = true;
            if (!base.GetValidateLoginRule(Gift.Model.ENUM.ERule.FreightTemplate))
            {
                base.Response.Write("<script language=javascript>window.top.location.href='../login.aspx';</script>");
                base.Response.End();
            }


        }

        protected void rptFreightWay_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            base.SetListPerssion(e, _isEdit, _isDeleted);

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
              
                string str = base.Request.Form["Item"];
                if (str.Contains(","))
                {
                    Array array = str.Split(new char[] { ',' });
                    foreach (string str2 in array)
                    {
                        oFreightWay = BaseFactory.theFreightWay.GetModel(Convert.ToInt32(str2));
                        BaseFactory.theFreightWay.Delete(Convert.ToInt32(str2));

                    }
                }
                else
                {
                    oFreightWay = BaseFactory.theFreightWay.GetModel(Convert.ToInt32(str));
                    BaseFactory.theFreightWay.Delete(Convert.ToInt32(str));

                }
            }
            catch
            {
            }
            base.Response.Redirect(HttpContext.Current.Request.Url.PathAndQuery);
        }

        protected override object LoadPageStateFromPersistenceMedium()
        {
            byte[] inArray = ViewStateCompression.DeCompress(Convert.FromBase64String(base.Request.Form.Get("__MSPVSTATE")));
            LosFormatter formatter = new LosFormatter();
            return formatter.Deserialize(Convert.ToBase64String(inArray));
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!base.IsPostBack)
            {
                this.ValidateLoginUser();
                this.DeleteItem();
                this.btnDelete.Attributes.Add("disabled", "disabled");
                this.FreightTemplateBind();
            }
        }

        void DeleteItem()
        {
            if (Request.QueryString["action"] == "del")
            {
                int id = int.Parse(Request.QueryString["id"]);
                try
                {
                    oFreightWay = BaseFactory.theFreightWay.GetModel(id);
                    BaseFactory.theFreightWay.Delete(id);
                    base.ClientScript.RegisterStartupScript(base.GetType(), "sd", "<script language=\"javascript\" defer>alert('删除成功！');</script>");
                }
                catch
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "sd", "<script language=\"javascript\" defer>alert('删除失败！');</script>");
                }

            }
        }



        protected override void SavePageStateToPersistenceMedium(object pViewState)
        {
            LosFormatter formatter = new LosFormatter();
            StringWriter output = new StringWriter();
            formatter.Serialize(output, pViewState);
            string hiddenFieldInitialValue = Convert.ToBase64String(ViewStateCompression.Compress(Convert.FromBase64String(output.ToString())));
            base.ClientScript.RegisterHiddenField("__MSPVSTATE", hiddenFieldInitialValue);
        }

    }
}